<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Import Namespace="Buffalo.Administration.DAL" %>
<%@ Import Namespace="Buffalo.Administration.Enum" %>
<%@ Import Namespace="Buffalo.SMECommon.DAL" %>
<%@ Import Namespace="Buffalo.SMECommon.Enum" %>
<%@ Import Namespace="Buffalo.Web.Areas.SMECommon" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            //ttt
            //if (!this.IsPostBack)
            //{
            //    bool hasEditPermission = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, "Employee", enuPermissionType.Edit);
            //    btnAdd.Visible = hasEditPermission;

            //    hidHasEditPermission.Value = hasEditPermission;
            //}
            Buffalo.Web.Areas.SMECommon.GeneralVar.gLanguageHelper.ApplyLanguage(this.mainViewPort, Buffalo.Web.Areas.SMECommon.CommonManager.GetCurrentCulture(), true);
        }
    </script>
    <script type="text/javascript">
        var openExportExcelHandler = function (IDOrgUnit, Criteria, SearchText) {
            var win = ExportExcelWindow;
            win.autoLoad.params.IDOrgUnit = IDOrgUnit;
            win.autoLoad.params.Criteria = Criteria;
            win.autoLoad.params.SearchText = SearchText;
            win.setTitle('Export Employee to excel');
            win.show();
        };
        var openImportDataHandler = function () {
            parent.AppGeneral.addTab({ title: 'Import data', url: '/Areas/SMECommon/Pages/ImportDataView.aspx', icon: 'Form' });

        };
        var openAddEmployeeHandler = function () {
            var win = EmployeeEditorWindow;
            win.autoLoad.params.IDEmployee = '0';
            win.setTitle('Add Employee');
            win.show();
        };
        var commandHandler = function (cmd, record) {
            switch (cmd) {
                case "edit":
                    var win = EmployeeEditorWindow;
                    win.autoLoad.params.IDEmployee = record.id;
                    win.setTitle('Edit Employee: ' + record.data.Code);
                    win.show();
                    break;
                case "delete":
                    if (hidHasEditPermission.getValue() != "true")
                        return;
                    Ext.Msg.confirm('Alert', 'Delete Employee?', function (btn) {
                        if (btn == "yes") {
                            dsEmployee.remove(record);
                            dsEmployee.save();
                        }
                    });
                    break;
            }
        };

        function render(value, metadata, record, rowIndex, colIndex, store) {
            if (colIndex < 2)
                return;
            var retVal;
            if (colIndex == 5) {
                if (value) {
                    var fullDate = value.toString().substring(0, 15);
                    var monthText = fullDate.substring(4, 7);
                    var month = GetMonthFromMonthText(monthText);
                    var day = fullDate.substring(8, 10);
                    var year = fullDate.substring(11, 15);
                    if (year != '1900')
                        retVal = day + "/" + month + "/" + year;
                    else
                        retVal = '';
                }
            }
            return retVal;
        }
        function GetMonthFromMonthText(monthText) {
            var monthNum = 0;
            switch (monthText) {
                case "Jan":
                    monthNum = "01";
                    break;
                case "Feb":
                    monthNum = "02";
                    break;
                case "Mar":
                    monthNum = "03";
                    break;
                case "Apr":
                    monthNum = "04";
                    break;
                case "May":
                    monthNum = "05";
                    break;
                case "Jun":
                    monthNum = "06";
                    break;
                case "Jul":
                    monthNum = "07";
                    break;
                case "Aug":
                    monthNum = "08";
                    break;
                case "Sep":
                    monthNum = "09";
                    break;
                case "Oct":
                    monthNum = "10";
                    break;
                case "Nov":
                    monthNum = "11";
                    break;
                case "Dec":
                    monthNum = "12";
                    break;
            }
            return monthNum;
        }
    </script>
</head>
<body>
    <ext:ResourceManager ID="ResourceManager1" runat="server" />
    <ext:Store ID="dsEmployee" runat="server" RemoteSort="true" UseIdConfirmation="false"
        AutoLoad="false">
        <Proxy>
            <ext:HttpProxy Url="/Employee/GetListEmployee/" />
        </Proxy>
        <UpdateProxy>
            <ext:HttpWriteProxy Url="/Employee/SaveEmployees/" />
        </UpdateProxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Code" />
                    <ext:RecordField Name="FirstName" />
                    <ext:RecordField Name="LastName" />
                    <ext:RecordField Name="PhoneNo" />
                    <ext:RecordField Name="Email" />
                    <ext:RecordField Name="MaritalStatus" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" />
            <ext:Parameter Name="sort" Value="FirstName" />
            <ext:Parameter Name="withDefaultItem" Value="false" Mode="Value" />
            <ext:Parameter Name="cboOrgUnit" Value="#{cboOrgUnit}.getValue()" Mode="Raw" />
            <ext:Parameter Name="cboCriteria" Value="#{cboCriteria}.getValue()" Mode="Raw" />
            <ext:Parameter Name="txtSearchText" Value="#{txtSearchText}.getValue()" Mode="Raw" />

        </BaseParams>
        <SortInfo Field="FirstName" Direction="ASC" />
    </ext:Store>

    <ext:Store ID="dsOrgUnit" runat="server" RemoteSort="true" UseIdConfirmation="false" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/SMECommon/Permission/GetListOrgUnitHierachy/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="Value" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="Value" />
                    <ext:RecordField Name="Text" />
                </Fields>
            </ext:JsonReader>

            </Reader>
        <SortInfo Field="Text" Direction="ASC" />
    </ext:Store>

    <ext:Viewport ID="mainViewPort" runat="server" Layout="fit">
        <Items>
            <ext:GridPanel ID="grdPanEmployeeList" runat="server" Header="false" Border="false"
                TrackMouseOver="true" ClicksToEdit="1" AutoExpandColumn="Email" StoreID="dsEmployee">
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:CommandColumn Width="25" Hideable="false">
                            <Commands>
                                <ext:GridCommand CommandName="edit" Icon="ApplicationFormEdit">
                                    <ToolTip Text="Edit" />
                                </ext:GridCommand>
                            </Commands>
                            <PrepareToolbar Handler="toolbar.setVisible(!record.newRecord);" />
                        </ext:CommandColumn>
                        <ext:CommandColumn Width="25" Hideable="false">
                            <Commands>
                                <ext:GridCommand CommandName="delete" Icon="Cross">
                                    <ToolTip Text="Delete" />
                                </ext:GridCommand>
                            </Commands>
                            <PrepareToolbar Handler="toolbar.setVisible(!record.newRecord);" />
                        </ext:CommandColumn>
                        <ext:Column ColumnID="Code" DataIndex="Code" Header="Code" />
                        <ext:Column ColumnID="FirstName" DataIndex="FirstName" Header="FirstName" />
                        <ext:Column ColumnID="PhoneNo" DataIndex="PhoneNo" Header="PhoneNo" />
                        <ext:Column ColumnID="Email" DataIndex="Email" Header="Email" />
                    </Columns>
                </ColumnModel>
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button ID="btnAdd" runat="server" Text="Add" Icon="Add">
                                <Listeners>
                                    <Click Handler="openAddEmployeeHandler();" />
                                </Listeners>
                            </ext:Button>
                            <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                            <ext:SelectBox ID="cboOrgUnit" runat="server" EmptyText="Select org unit..." StoreID="dsOrgUnit" DisplayField="Text" ValueField="Value">
                            </ext:SelectBox>
                            <ext:SelectBox ID="cboCriteria" runat="server" EmptyText="Select criteria...">
                                <Items>
                                    <ext:ListItem Text="..." Value="ALL" />
                                    <ext:ListItem Text="Code" Value="Code" />
                                    <ext:ListItem Text="Name" Value="Name" />
                                    <ext:ListItem Text="Note" Value="Note" />
                                </Items>
                            </ext:SelectBox>
                            <ext:ToolbarSpacer ID="ToolbarSpacer2" runat="server" />
                            <ext:TextField ID="txtSearchText" runat="server" AnchorHorizontal="100%" EmptyText="Search..." />
                            <ext:ToolbarSpacer ID="ToolbarSpacer3" runat="server" />
                            <ext:Button ID="btnSearch" runat="server" Text="Search" Icon="Find">
                                <Listeners>
                                    <Click Handler="dsEmployee.reload();" />
                                </Listeners>
                            </ext:Button>
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <SelectionModel>
                    <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" />
                </SelectionModel>
                <BottomBar>
                    <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="15" />
                </BottomBar>
                <Listeners>
                    <Command Fn="commandHandler" />
                </Listeners>
                <LoadMask ShowMask="true" />
                <SaveMask ShowMask="true" />
            </ext:GridPanel>
            <ext:Hidden ID="hidHasEditPermission" runat="server">
            </ext:Hidden>
        </Items>
    </ext:Viewport>
    <ext:Window ID="EmployeeEditorWindow" runat="server" Icon="ApplicationFormEdit" Width="1000"
        Height="500" Hidden="true" Modal="true" Constrain="true">
        <AutoLoad Url="/SMECommon/Employee/EmployeeEditor/" Mode="IFrame" TriggerEvent="show"
            ReloadOnEvent="true" ShowMask="true" MaskMsg="Loading Employee ...">
            <Params>
                <ext:Parameter Name="IDEmployee" Value="" Mode="Value" />
            </Params>
        </AutoLoad>
    </ext:Window>
</body>
</html>
